"""Create fasta file of membrane training data by giving the folder name.

For the structure prediction course membrane protein training data was
supplied. This script writes all the sequences in those *.txt files to one
fasta file. Used for blastclust.
Usage:
    python fasta_generator.py ../data/membrane_training_data/
"""
import sys
import getopt
import os
import glob

def process(trainingpath):
    """Print the sequences of all the files in the path in fasta format."""
    for trainingfile in glob.glob(os.path.join(trainingpath, '*.txt')):
        print '>' + trainingfile.split('/')[-1][:-4]
        tfh = open(trainingfile, 'r')
        tfh.readline()
        sequence = ''
        for line in tfh:
            columns = line.split()
            sequence += columns[1]
        print sequence
        tfh.close()

def main():
    # parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
    except getopt.error, msg:
        print msg
        print "for help use --help"
        sys.exit(2)
    # process options
    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            sys.exit(0)
    # process arguments
    if len(args) == 1:
        process(args[0]) # process() is defined elsewhere
    else:
        print __doc__

if __name__ == "__main__":
    main()
